<?php

namespace Lib;

final class Response {
    //    use \Lib\Traits\StaticClass;

    /**
     * @desc 成功返回信息并跳转
     *
     * @param string $msg
     * @param string $url
     * @return bool
     */
    public static function msg($msg = '编辑成功', $url = '') {
        $router               = \Phalcon\Di::getDefault()->get("router");
        $moduleName           = $router->getModuleName();
        $controllerName       = $router->getControllerName();

        if (empty($url)) {
            $url      = '/' . $moduleName . '/' . $controllerName . '/list';
            $resource = ['status' => true, 'msg' => $msg, 'url' => $url];
        } else {
            $resource = ['status' => true, 'msg' => $msg, 'url' => '/' . $moduleName . $url];
        }

        echo \json_encode($resource);
        return false;
    }

    public static function sucmsg($data = [], $msg = 'ok') {
        $resource = ['status' => true, 'msg' => $msg, "data" => $data];
        echo \json_encode($resource);
        return false;
    }

    /**
     * @desc 失败返回信息
     *
     * @param string $msg
     * @return bool
     */
    public static function errmsg($msg = '编辑失败', $errorCode=10000) {
        $resource = ['status' => false, 'msg' => $msg, "errorCode" => $errorCode];
        echo \json_encode($resource);
        return false;
    }

    public static function jsonp($data, $status = true, $code = 200, $msg = 'success') {
        $request  = new \Phalcon\Http\Request();
        $callback = $request->getQuery('callback');

        if (empty($callback)) {
            return $status ? self::data($data, $msg, $code) : self::error($data ? : $msg, $code);
        }

        $resource = [
            'status' => $status,
            'code'   => (int)$code,
            'msg'    => $msg,
        ];

        //$resource['data'] = is_array($data)
        //    ? $data
        //    : ($data instanceof \Base\Model\AbstractModel ? $data->toArray() : (string)$data);
        if (is_array($data)) {
            $resource['data'] = $data;
        } else {
            return self::error($data ? : $msg, $code);
        }

        return self::callback($resource, $callback);
    }

    public static function data($data, $msg = '', $code = 200) {
        $resource        = ['status' => true, 'code' => (int)$code];
        $resource['msg'] = $msg;
        //$resource['data'] = is_array($data)
        //    ? $data
        //    : ($data instanceof \Base\Model\AbstractModel ? $data->toArray() : (string)$data);
        if (is_array($data)) {
            $resource['data'] = $data;
        } else {
            return self::error($data ? : $msg, $code);
        }

        header('Content-type: application/json; charset=utf-8');
        echo \json_encode($resource, JSON_UNESCAPED_UNICODE);

        return false;
    }

    public static function error($msg = '', $code = 0) {
        $resource = [
            'status' => false,
            'code'   => (int)$code,
            'msg'    => (string)$msg,
        ];

        header('Content-type: application/json; charset=utf-8');
        echo \json_encode($resource, JSON_UNESCAPED_UNICODE);

        return false;
    }

    public static function callback($data, $callback) {
        if (!!$callback && preg_match('/^[a-zA-Z_][a-zA-Z0-9_\.]*$/', (string)$callback)) {
            //$resource = is_array($data)
            //    ? $data
            //    : ($data instanceof \Base\Model\AbstractModel ? $data->toArray() : (string)$data);
            if (is_array($data)) {
                $resource['data'] = $data;
            } else {
                $resource['data'] = '';
            }

            header('Access-Control-Allow-Origin:*');
            header('Access-Control-Allow-Methods:POST');
            header('Access-Control-Allow-Headers:x-requested-with,content-type');
            echo $callback . '(' . \json_encode($resource) . ');';

            return false;
        }

        return self::error('invalid callback');
    }

}
